package com.wlb.es.service.impl;

import com.wlb.es.entity.User;
import com.wlb.es.service.UserService;
import com.wlb.es.util.EsUtil;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    EsUtil esUtil;

    @Autowired
    @Qualifier("restHighLevelClient")//使用@Quanlifier注解告诉Spring哪个bean应该被自动注入
    RestHighLevelClient client;

    /**
     * 创建索引
     */
    @Override
    public void createIndex(String indexName) {
        esUtil.createIndex(indexName);
    }

    /**
     * 添加用户
     */
    @Override
    public void add(User user,String indexName) {
        esUtil.insertDoc(user,indexName);
    }

    /**
     * 批量添加用户
     */
    public void addUsers(List<User> users,String indexName){
        esUtil.addUsers(users,indexName);
    }

    /**
     * 查询一个user
     */
    @Override
    public User findById(String id,String indexName){
        return esUtil.findById(id,indexName);
    }

    /**
     * 查询所有用户
     */
    @Override
    public List<User> findAll(String indexName) {
        return esUtil.searchDoc(indexName);
    }

    /**
     * 分页查询
     */
    @Override
    public List<User> pageQuery(String indexName,Integer page,Integer size){
        return esUtil.pageQuery(indexName,page,size);
    }

    /**
     * 删除用户
     */
    @Override
    public void deleteById(String id,String indexName){
        esUtil.deleteById(id,indexName);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteByIds(List<String> ids,String indexName){
        esUtil.deleteByIds(ids,indexName);
    }
    /**
     * 修改用户
     */
    @Override
    public void updateById(User user,String indexName){
        esUtil.updateById(user,indexName);
    }
}
